SpringBoot官方文档翻译(十一):使用Spring Boot&Maven

Part III. Using Spring Boot(使用Spring Boot)

1
2
3
4
5
6
7
This section goes into more detail about how you should use     
Spring Boot. It covers topics such as build systems, auto-configuration,
and how to run your applications. We also cover some Spring Boot
best practices. Although there is nothing particularly special about
Spring Boot (it is just another library that you can consume),
there are a few recommendations that, when followed, make your
development process a little easier.

本部分介绍了更多的关于您如何使用Spring Boot的细节。它涵盖了构建系统,自动配置,如何运行您的应用等主题。我们同样涉及了Spring Boot的最佳实践。尽管Spring Boot并没有什么非常特别的东西(它仅仅是您可消费的另一个库而已),接下去,有一些建议,可以使您的部署进程变得更加简单。

13. Build Systems(构建系统)

1
2
3
4
5
6
It is strongly recommended that you choose a build system that     
supports dependency management and that can consume artifacts
published to the “Maven Central” repository. We would recommend
that you choose Maven or Gradle. It is possible to get Spring Boot
to work with other build systems (Ant, for example), but they are
not particularly well supported.

我们强烈推荐您选择一款构建系统支持依赖管理的,并且能够发布到“Maven Central”库。我们推荐您选择Maven或者Gradle。当然,Spring Boot也同样能够作用于诸如Ant等构建工具,但是对于他们的支持并不是全面的。

13.1 Dependency Management(依赖管理)

1
2
3
4
5
6
Each release of Spring Boot provides a curated list of     
dependencies that it supports. In practice, you do not
need to provide a version for any of these dependencies
in your build configuration, as Spring Boot manages that
for you. When you upgrade Spring Boot itself, these
dependencies are upgraded as well in a consistent way.

Spring Boot的每个发行版都提供了一个它支持的依赖关系的策划列表。基于实践,在您的构建配置中您不需要为任何依赖提供一个版本。因为Spring Boot为您管理这些版本。当您升级Spring Boot本身的时候,这些依赖版本会以一致的方式很好的自动升级。

1
2
You can still specify a version and override Spring Boot’s     
recommendations if you need to do so.

如果您愿意的话,您依然可以指定一个版本覆盖Spring Boot建议的版本。

1
2
3
4
The curated list contains all the spring modules that you can use     
with Spring Boot as well as a refined list of third party libraries.
The list is available as a standard Bills of Materials (spring-boot-
dependencies) that can be used with both Maven and Gradle.

这个策划列表包含了所有的您可以配合Spring Boot 使用的spring模块和一个精简的第三方库。
这个列表可以作为一个标准的材料清单(spring-boot-dependencies)用于Maven或者Gradle。

1
2
Each release of Spring Boot is associated with a base version of the     
Spring Framework. We highly recommend that you not specify its version

每一个发布的Spring Boot版本会关联Spring的一个基础版本。我们高度推荐您不要自定义它的版本。

13.2 Maven

1
2
3
Maven users can inherit from the spring-boot-starter-parent     
project to obtain sensible defaults. The parent project
provides the following features:

Maven用户可以继承spring-boot-starter-parent 工程去获得合理的默认值。该父工程提供了如下的功能:

1
2
3
4
5
6
7
8
9
•	Java 1.8 as the default compiler level.
• UTF-8 source encoding.
• A Dependency Management section, inherited from the spring-boot-dependencies
pom, that manages the versions of common dependencies. This dependency management
lets you omit <version> tags for those dependencies when used in your own pom.
• Sensible resource filtering.
• Sensible plugin configuration (exec plugin, Git commit ID, and shade).
• Sensible resource filtering for application.properties and application.yml including profile-specific files
(for example, application-dev.properties and application-dev.yml)
  • java 1.8作为默认的编译等级
  • UTF-8作为源编码字符集
  • 依赖管理部分,继承于spring-boot-dependencies pom管理基础依赖版本。这种依赖管理能够让您在您的pom中忽略标签。
  • 恰到好处的资源过滤
  • 恰如其分的插件配置(exec plugin, Git commit ID, and shade)
  • 通过profile—specific为application.propertiesapplication.yml做恰当的资源文件过滤(例如application-dev.properties 和 application-dev.yml)
1
2
3
4
Note that, since the application.properties and application.yml     
files accept Spring style placeholders (${… }), the Maven filtering
is changed to use @..@placeholders. (You can override that by setting
a Maven property called resource.delimiter.)

注意,因为application.propertiesapplication.yml文件接收Spring风格的占位符(${… }),Maven过滤器变成使用@..@占位符。(您可以通过resource.delimiter文件设置Maven的属性覆盖maven的占位符。

13.2.1 Inheriting the Starter Parent(继承父Starter)

1
2
To configure your project to inherit from the     
spring-boot-starter-parent, set the parent as follows:

配置您的工程去继承spring-boot-starter-parent,设置父pom如下:

1
2
3
4
5
6
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.BUILD-SNAPSHOT</version>
</parent>
1
2
3
You should need to specify only the Spring Boot version number     
on this dependency. If you import additional starters, you can
safely omit the version number.

你仅仅只需要再依赖中定义Spring Boot的版本号。如果您想导入额外的starters,您可以安全的忽略版本号。

1
2
3
4
With that setup, you can also override individual dependencies     
by overriding a property in your own project. For instance, to
upgrade to another Spring Data release train, you would add the
following to your pom.xml:

用那个设置,您依然可以通过复写属性覆盖个人的依赖,例如,升级到另一个Spring Data的升级版,您可以增加以下配置到您的pom.xml中:

1
2
3
<properties>
<spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version>
</properties>

1
Check the spring-boot-dependencies pom for a list of supported properties.

检查spring-boot-dependencies pom(https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-dependencies/pom.xml)以获取支持的属性列表

13.2.2 Using Spring Boot without the Parent POM(不使用父POM)

1
2
3
Not everyone likes inheriting from the spring-boot-starter-parent     
POM. You may have your own corporate standard parent that you need
to use or you may prefer to explicitly declare all your Maven configuration.

并不是每个人都喜欢用继承于spring-boot-starter-parent POM的方式。您可能需要使用您自己的团队标准的父POM,又或者您更愿意明确声明您所有的Maven配置

1
2
3
If you do not want to use the spring-boot-starter-parent, you can     
still keep the benefit of the dependency management (but not the
plugin management) by using a scope=import dependency, as follows:

如果您不愿使用spring-boot-starter-parent,您仍然可以通过使用scope=import依赖来保留依赖管理的价值,如下:

1
2
3
4
5
6
7
8
9
10
11
12
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.0.BUILD-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
1
2
3
4
5
6
The preceding sample setup does not let you override individual     
dependencies by using a property, as explained above. To achieve
the same result, you need to add an entry in the dependencyManagement 
of your project before the spring-boot-dependencies entry.
For instance, to upgrade to another Spring Data release train,
you could add the following element to your pom.xml:

如上所述,上述示例设置不会让您通过使用属性覆盖个人依赖关系。为了达到同样目标,您需要在dependencyManagement中的spring-boot-dependencies前使用一个节点。举个例子,升级另一个Spring Data 发行版本,您可以增加如下的元素到您的pom.xml中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<dependencyManagement>
<dependencies>
<!-- Override Spring Data release train provided by Spring Boot -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-releasetrain</artifactId>
<version>Fowler-SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.0.BUILD-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

1
2
In the preceding example, we specify a BOM, but any     
dependency type can be overridden in the same way.

在之前的例子中,我们定义了一个清单,但是任何的依赖类型都能够以这种方式进行覆盖。

13.2.3 Using the Spring Boot Maven Plugin

1
2
3
Spring Boot includes a Maven plugin that can package the project     
as an executable jar. Add the plugin to your <plugins> section if
you want to use it, as shown in the following example:

Spring Boot引入了一个Maven的插件,可以打包工程,使之成为一个可执行jar。如果您想使用它的话,增加该插件到部分。如下展示的示例:

1
2
3
4
5
6
7
8
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
1
2
3
If you use the Spring Boot starter parent pom, you need     
to add only the plugin. There is no need to configure it
unless you want to change the settings defined in the parent.

如果您使用父POM的方式,您仅仅只需要增加这个插件就可以了。不需要配置它,除非您想改变父POM定义的配置。

分享到